package com.cmcc.linlaker;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.cmcc.linlaker.bean.UpdateBean;
import com.cmcc.linlaker.http.DownloadService;
import com.cmcc.linlaker.http.VersionUpdateImpl;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.UpdateManager;
import com.cmcc.linlaker.view.UpdateDialog;
import com.cmcc.linlaker.view.UpdateProgressDialog;

import java.io.File;
import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;


public class LoadingActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {
    public static int screenWidth;// 屏幕宽度，单位为px
    public static int screenHeight;// 屏幕高度，单位为px

    /**
     * 当前设备与给定图片的基准设备放大或缩小倍数
     */
    public static double scaleRate_W;
    public static double scaleRate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!isTaskRoot()) {
            final Intent intent = getIntent();
            final String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent
                    .ACTION_MAIN)) {
                finish();
                return;
            }
        }

        setContentView(R.layout.activity_loading);

        BarUtils.initStatusBar(this);
        getScreenMatrix();

        PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,
                "tZ6EyIotPQ6huMeLsGmK2ReN");

        locationTask();
//        initView();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        initView();
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        finish();
    }

    private static final String[] LOCATION =
            {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final int RC_LOCATION_CONTACTS_PERM = 124;

    public void locationTask() {
        if (hasLocationPermissions()) {
            initView();
        } else {
            EasyPermissions.requestPermissions(
                    this,
                    "临湖监管APP需要获取位置权限",
                    RC_LOCATION_CONTACTS_PERM,
                    LOCATION);
        }
    }

    private boolean hasLocationPermissions() {
        boolean isHasLocationPermission = EasyPermissions.hasPermissions(this, LOCATION);
        return isHasLocationPermission;
    }

    private void initView() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(LoadingActivity.this,
                        LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }, 3000);
    }

    /**
     * 获取屏幕分辨率大小
     */
    private void getScreenMatrix() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;

        /** 屏幕放大和缩小的宽高四舍五入计算 */
        scaleRate_W = screenWidth * 1.0 / 750;
        scaleRate = screenHeight * 1.0 / 1334;
//        SPHelper.setScaleRate((float) scaleRate);
//        SPHelper.setScaleRate_W((float) scaleRate_W);

        System.out.println("screenW=" + screenWidth + " screenH="
                + screenHeight + " scaleRate_W=" + scaleRate_W
                + " scaleRate_H=" + scaleRate);
    }


    @Override
    public void onClick(View v) {
    }

}
